<template>
  <el-dialog title="操作" :close-on-click-modal="false" width="600px" :visible.sync="visible">
    <el-form ref="temp" :model="formData" label-width="80px" @keyup.enter.native="handleSubmit()">
      <el-form-item label="请假类型">
        <el-input v-model="mdl.leaveType" :disabled="true" placeholder="" />
      </el-form-item>
      <el-form-item label="标题">
        <el-input v-model="mdl.title" :disabled="true" placeholder="" />
      </el-form-item>
      <el-form-item label="描述">
        <el-input v-model="mdl.description" :disabled="true" type="textarea" placeholder="" />
      </el-form-item>
      <el-form-item label="起止日期">
        <span> {{ mdl.startDate }} &nbsp;&nbsp;至&nbsp;&nbsp; {{ mdl.endDate }}</span>
      </el-form-item>
      <el-form-item label="时长">
        <span>{{ mdl.duration }} 小时</span>
      </el-form-item>
      <el-form-item v-show="audit" label="审批意见">
        <el-input
          v-model="formData.comment"
          :disabled="true"
          :autosize="{ minRows: 2, maxRows: 4}"
          type="textarea"
          placeholder=""
        />
      </el-form-item>
    </el-form>
    <span v-show="audit" slot="footer" class="dialog-footer">
      <el-button @click="handleSubmit(3)">
        驳回
      </el-button>
      <el-button type="primary" @click="handleSubmit(2)">
        同意
      </el-button>
    </span>
  </el-dialog>

</template>
<script>
import { getDictMap } from '@/utils/dict'
import { getLeaveByBusiness, auditTask } from '@/api/activiti'
export default {
  name: 'LeaveModal',
  components: {

  },
  props: {
    audit: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      mdl: {},
      options: [],
      dateFormat: 'YYYY-MM-DD HH:mm',
      formData: {},
      leaveTypeMap: {},
      form: '' // this.$form.createForm(this)
    }
  },
  watch: {
    /*
              'selectedRows': function (selectedRows) {
                this.needTotalList = this.needTotalList.map(item => {
                  return {
                    ...item,
                    total: selectedRows.reduce( (sum, val) => {
                      return sum + val[item.dataIndex]
                    }, 0)
                  }
                })
              }
              */
  },
  beforeCreate() {
    getDictMap('act_leave_type').then(res => {
      this.leaveTypeMap = res
    })
  },
  created() {
  },
  methods: {
    show(record) {
      // record 是任务 如果只是查看可以只传入一个businessKey
      if (this.audit) {
        this.formData = Object.assign(
          {
            taskId: record.id,
            procInstId: record.procInstId,
            procName: record.processDefName,
            businessKey: record.businessKey,
            procDefKey: record.processDefKey,
            applyer: record.applyer
          }
        )
      }
      getLeaveByBusiness(record.businessKey).then(res => {
        if (res.code === 0) {
          const { type } = res.data
          this.mdl = Object.assign(res.data)
          this.mdl.leaveType = this.leaveTypeMap.get(type.toString())
          this.visible = true
        }
      })
    },
    handleSubmit(result) {
      this.confirmLoading = true
      const formData = this.formData
      formData.result = result
      auditTask(formData).then(res => {
        if (res.code === 0) {
          this.$message.success('保存成功')
          this.$emit('ok')
          this.visible = false
        } else {
          this.$message.success(res.msg)
        }
      }).catch(() => {
        this.$message.error('系统错误，请稍后再试')
      }).finally(() => {
        this.confirmLoading = false
      })
    }
  }
}
</script>

 <style scoped>
</style>
